﻿using System;
using System.Data.SqlClient;
using System.Web.UI;
using UDS.Components;
using UDS.Web.Pages;

namespace UDS.SubModule.Role
{
    /// <summary>
    /// ORole 的摘要说明。
    /// </summary>
    public partial class ORolePage : Page
    {
        private string RoleDescription = "";
        private string RoleName = "";

        protected PageDisplayType DisplayType
        {
            get;
            private set;
        }

        protected int RoleID
        {
            get;
            private set;
        }

        protected override void OnInit(EventArgs e)
        {
            cmdModify.ServerClick += cmdModify_ServerClick;
            cmdDelete.ServerClick += cmdDelete_ServerClick;
            cmdAdd.ServerClick += cmdAdd_ServerClick;
            base.OnInit(e);
        }

        protected override void OnLoad(EventArgs e)
        {
            int value;
            if (!int.TryParse(Request.QueryString["Role_ID"], out value))
            {
                value = 0;
            }
            RoleID = value;

            if (!int.TryParse(Request.QueryString["DisplayType"], out value))
            {
                value = 0;
            }
            DisplayType = (PageDisplayType)value;

            if (!Page.IsPostBack)
            {
                txtMRoleName.Text = "";

                var myRole = new Components.Role();
                using (SqlDataReader dr = myRole.GetRoleInfo(RoleID))
                {
                    while (dr.Read())
                    {
                        RoleName = dr["Role_Name"].ToString();
                        RoleDescription = dr["Role_Description"].ToString();
                    }
                    dr.Close();
                }
            }

            // 显示不同表格
            switch (DisplayType)
            {
                case PageDisplayType.Add:
                    tabAdd.Visible = true;
                    tabDelete.Visible = false;
                    tabModify.Visible = false;
                    break;

                case PageDisplayType.Delete:
                    tabAdd.Visible = false;
                    tabDelete.Visible = true;
                    tabModify.Visible = false;
                    delRoleName.Text = RoleName;
                    break;

                case PageDisplayType.Edit:
                    tabAdd.Visible = false;
                    tabDelete.Visible = false;
                    tabModify.Visible = true;
                    if (txtMRoleName.Text.Trim() == "")
                    {
                        txtMRoleName.Text = RoleName;
                        txtMRoleDescription.Text = RoleDescription;
                    }
                    break;
            }

            base.OnLoad(e);
        }

        private void cmdAdd_ServerClick(object sender, EventArgs e)
        {
            if (Page.IsValid)
            {
                if (Components.Role.Add(txtARoleName.Text, txtARoleDescription.Text) < 0)
                {
                    Logger.Log("添加角色出错");
                    Response.Redirect("../Error.aspx");
                }
                else
                {
                    Response.Redirect("ListView.aspx?Role_ID=" + RoleID + "&Refresh=1");
                }
            }
        }

        private void cmdDelete_ServerClick(object sender, EventArgs e)
        {
            if (Page.IsValid)
            {
                if (Components.Role.Delete(RoleID) != 0)
                {
                    Logger.Log("添加角色出错");
                    Response.Redirect("../Error.aspx");
                }
                else
                {
                    Response.Redirect("ListView.aspx?Role_ID=" + RoleID + "&Refresh=1");
                }
            }
        }

        private void cmdModify_ServerClick(object sender, EventArgs e)
        {
            if (Page.IsValid)
            {
                if (Components.Role.Modify(RoleID, txtMRoleName.Text, txtMRoleDescription.Text) != 0)
                {
                    Logger.Log("添加角色出错");
                    Response.Redirect("../Error.aspx");
                }
                else
                {
                    Response.Redirect("ListView.aspx?Role_ID=" + RoleID + "&Refresh=1");
                }
            }
        }
    }
}